<?php
/**
 * UseradminController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';
class Admin_UserController extends Sunnet_Controller_Action
{
	var $_arrParam;
	public function init()
    {
//    	Truyền các tham số nhận được ra view
    	$this->_arrParam = $this->_request->getParams();
    	$this->view->arrParam = $this->_arrParam;
    	if (!$this->_arrParam['page']) {
    		$this->_arrParam['page'] = 1;
    	}
    	if (!$this->_arrParam['line_per_page']) {
    		$this->_arrParam['line_per_page'] = 10;
    	}
         Zend_Loader::loadClass('Admin_Models_MenuModel');
         $db = new Admin_Models_MenuModel();
         $mainmenu = $db->loadmainmenu();
         $this->view->mainmenu = $mainmenu; 
         
    }

    public function preDispatch(){
		$template_path = TEMPLATE_PATH . "/admin/default/";
		$this->loadTemplate($template_path,'template.ini','template');
    	$auth = Zend_Auth::getInstance();
    	if($this->_arrParam['action'] =="build-user-grown"){
    		$this->_helper->layout()->disableLayout();
    	}else if (!$auth->hasIdentity() || !$auth->getIdentity()->admin) {
    		$this->_forward('login','index','admin');//,$this->_arrParam);
    	}
        Zend_Loader::loadClass('Admin_Models_MenuModel');
         $db = new Admin_Models_MenuModel();
         $mainmenu = $db->loadmainmenu();
         $this->view->mainmenu = $mainmenu;
    }
    /**
     * The default action - show the home page
     */
    public function userFilterAction(){
        
    }
    function rangeMonth($datestr) {
    	date_default_timezone_set(date_default_timezone_get());
    	$dt = strtotime($datestr);
    	$res['start'] = date('Y-m-d', strtotime('first day of this month', $dt));
    	$res['end'] = date('Y-m-d', strtotime('last day of this month', $dt));
    	return $res;
    }
    
    function rangeWeek($datestr) {
    	date_default_timezone_set(date_default_timezone_get());
    	$dt = strtotime($datestr);
    	$res['start'] = date('N', $dt)==1 ? date('Y-m-d', $dt) : date('Y-m-d', strtotime('last monday', $dt));
    	$res['end'] = date('N', $dt)==7 ? date('Y-m-d', $dt) : date('Y-m-d', strtotime('next sunday', $dt));
    	return $res;
    }
    public function indexAction ()
    {
    	
//    	Load model và form sử dụng trong action


    	Zend_Loader::loadClass('Admin_Forms_UserSearchForm');
    	Zend_Loader::loadClass('Admin_Models_UserModel');
    	$db_game = new Admin_Models_UserModel();
    	$listgame = $db_game->loadlistgame();
// 		Zend_debug::dump($listgame);
// 		die;
//    	Khai báo biến

    	$search_form = new Admin_Forms_UserSearchForm($listgame,array('submit'	=> 'Tìm kiếm'));
    	
//    	Khai báo biến

    	$db = Zend_Db_Table::getDefaultAdapter();
    	$mySql = $db->select()
    							->from(array('u'	=> 'user'),array('create_time'))
    							->join(array('ui'	=> 'userinfo'),'ui.UserID = u.UserID',array('UserID','Username','Birthday','Contact Phone','Email','Gender','via'))
    							->order('u.create_time DESC')
    							->where('u.status = 1');
    	$request = $this->getRequest();
//     	Zend_Debug::dump($this->_arrParam);
    	//die;
    	if(isset($this->_arrParam['Title'])){
    	    //Zend_debug::dump($this->_arrParam);
    		if($this->_arrParam['Title']=='0'){
    		    $dk1 = '';
    		}else{
    		    $dk1 = "via LIKE '%".$this->_arrParam['Title']."%'";
    		}
    		if($this->_arrParam['type'] == '0'){
    		    $dk2 = '';    			
    		}else{
    		     $dk2 ='ui.`'.$this->_arrParam['type'] ."` like '%".$this->_arrParam['search']."%'";
    		}
    		
    		if($this->_arrParam['time_search']=='0'){
    		    $dk3= "u.create_time BETWEEN '".$this->_arrParam['Starttime']."' AND '".$this->_arrParam['Endtime']."'"  ;
    		}else if($this->_arrParam['time_search']=='1'){
    		    $dk3= "u.create_time BETWEEN '".date('Y-m-d')." 00:00:00' AND '".date('Y-m-d')." 23:59:59'"  ; 
    		}else if($this->_arrParam['time_search']=='2'){
    		    $data_week = $this->rangeWeek(date('Y-m-d'));
    		    $dk3= "u.create_time BETWEEN '".$data_week['start']."' AND '".$data_week['end']."'"  ;    		    
    		}else if($this->_arrParam['time_search']=='3'){
    		    $data_month = $this->rangeMonth(date('Y-m-d'));
    		    $dk3= "u.create_time BETWEEN '".$data_month['start']."' AND '".$data_month['end']."'"  ;
    		}else if($this->_arrParam['time_search']=='4'){
    		    $dk3="";
    		}
    			if($dk1!="")  $mySql->where($dk1);
    			if($dk2!="")  $mySql->where($dk2);
    			if($dk3!="")  $mySql->where($dk3);
    			
    			
    		
//     		Zend_Debug::dump($this->_arrParam);
//     		Zend_debug::dump($dk1);
//     		Zend_debug::dump($dk2);
//     		Zend_debug::dump($dk3);
//     		echo $mySql;
//     		die;
    		$this->view->arrParams = $this->_arrParam;
    	}
    	
    	//Zend_debug::dump($this->_arrParam);
    	if(isset($this->_arrParam['line_per_page'])){
    	 	 $this->setPagination($mySql, $this->_arrParam,$this->_arrParam['line_per_page']);
    	}else if(!isset($this->_arrParam['line_per_page'])){
    	    $this->setPagination($mySql, $this->_arrParam,10);
    	}
   	      $this->view->arrParam = $this->_arrParam;
//    	Truyền biến ra view
		
    	$search_form->setDefaults($this->_arrParam);
    	$this->view->search_form = $search_form;
    	
    }
//    public function addAction(){
////    	Load model sử dụng trong action
//
//    	Zend_Loader::loadClass('Admin_Forms_UserForm');
//    	Zend_Loader::loadClass('Admin_Models_UserPropertiesModel');
//    	Zend_Loader::loadClass('Admin_Models_UserModel');
//    	Zend_Loader::loadClass('Admin_Models_CpModel');
//    	
////    	Khai báo biến
//
//    	$cp_model = new Admin_Models_CpModel();
//    	$form = new Admin_Forms_UserForm(array('submit'	=> 'Thêm','cancel_link'	=> '/admin/cp','cp_list'	=> $cp_model->getAllCpList()));
//    	$user_properties_model = new Admin_Models_UserPropertiesModel();
//    	$user_model = new Admin_Models_UserModel();
//    	
////    	Nếu action là post thì thực hiện thêm nhà phát hành
//
//    	if ($this->_request->isPost()){
//    		
////    		Kiểm tra dữ liệu nhập vào đã thỏa hết các điều kiện chưa
//
//    		if ($form->isValid($this->_arrParam)){
//    			try {
//    				$clause    = $user_properties_model->getAdapter()->quoteInto('type = ?', $this->_arrParam['type']);
//					$validator = new Zend_Validate_Db_NoRecordExists(
//					    array(
//					        'table'   => 'userproperties',
//					        'field'   => 'username',
//					        'exclude' => $clause
//					    )
//					);
//					$data = $form->getValues();
//					if ($validator->isValid($data['username'])){
//						$user_properties_data = array();
//						$user_data = array();
//						$user_properties_data['username'] = $data['username'];
//						$user_properties_data['fullName'] = $data['fullName'];
//						$user_properties_data['mobile'] = $data['mobile'];
//						$user_properties_data['email'] = $data['email'];
//						$user_properties_data['birthday'] = $data['birthday'];
//						$user_properties_data['sex'] = $data['sex'];
//						$user_properties_data['cmtnd'] = $data['cmtnd'];
//						$user_properties_data['type'] = $data['type'];
//						$user_properties_data['cpid'] = $data['cpid'];
//						$user_data['username'] = $data['username'];
//						$user_data['fullName'] = $data['fullName'];
//						$user_data['password'] = $data['password'];
//						$user_data['passwordEncription'] = sha1($data['passwordEncription']);
//						$user_data['type'] = $data['type'];
//	    				$user_properties_model->insert($user_properties_data);
//	    				$user_model->insert($user_data);
//	    				$this->_redirect('/admin/user');
//					}else{
//						$username_element = $form->getElement('username');
//						$username_element->addError("Tài khoản đã tồn tại");
//						$form->addElement($username_element,'username');
//					}
//    			} catch (Exception $e) {
//    				echo $e;
//    			}
//    		}
//    	}
////    	Truyền biến ra view
//
//    	$this->view->form = $form;
//    }
//    public function editAction(){
////    	Load form và model sử dụng trong action
//
//    	Zend_Loader::loadClass('Admin_Models_UserPropertiesModel');
//    	Zend_Loader::loadClass('Admin_Forms_EditUserForm');
//    	
////    	Khai báo biến
//
//    	$model = new Admin_Models_UserPropertiesModel();
//    	$form = new Admin_Forms_EditUserForm(array('submit'	=> 'Chấp nhận','cancel_link'	=> '/admin/user'));
//		$where[] = $model->getAdapter()->quoteInto('username = ?', $this->_arrParam['username']);
//		$where[] = $model->getAdapter()->quoteInto('type = ?', $this->_arrParam['type']);
//    	
////    	Sửa số tiền của user
//
//		if ($this->_request->isPost()){
//			try {
//				$model->update(array('money'	=> new Zend_Db_Expr('money + ' . $this->_arrParam['money'])), $where);
//	    		$this->_redirect('/admin/user');
//	    	} catch (Exception $e) {
//	    		echo $e;
//	    	}
//		}
//		
////		Truyền biến ra view
//
//		$this->view->form = $form;
//		$this->view->user_info = $model->fetchRow($where)->toArray();
//    }
    public function deleteAction(){
//    	Load model sử dụng trong action

    	Zend_Loader::loadClass('Admin_Models_UserModel');
    	
//    	Khai báo biến

    	$model = new Admin_Models_UserModel();
		$where[] = $model->getAdapter()->quoteInto('UserID = ?', $this->_arrParam['user_id']);
		
//		Tắt view

		$this->_helper->viewRenderer->setNoRender();
//    	Xóa record

		try {
			$model->update(array('status'	=> 0), $where);
//    		$model->delete($where);
    		$this->_redirect('/admin/user');
    	} catch (Exception $e) {
    		echo "Có lỗi trong quá trình xử lý. Bạn vui lòng thử lại sau";
//    		echo $e;
    	}
    }
    
    public function growthAction(){
        Zend_Loader::loadClass('Admin_Forms_UserGrowthForm');
    	Zend_Loader::loadClass('Admin_Models_UserModel');
    	$db_game = new Admin_Models_UserModel();
    	$listgame = $db_game->loadlistgame();
    	
    	$this->view->headScript()->appendFile('/js/highstock.js');
    	
    	
		//Zend_debug::dump($this->_arrParam);
		//die;
// 		die;
//    	Khai báo biến
		$arr = array();
    	if($this->_arrParam['time_search']!="0"){
    	    $this->_arrParam['Starttime']=null;
    	    $this->_arrParam['Endtime']=null;
    	}
    	$search_form = new Admin_Forms_UserGrowthForm($listgame,array('submit'	=> 'Xem'));
    	$search_form->setDefaults($this->_arrParam);
    	$this->view->arrParams = $this->_arrParam;
    	$this->view->search_form = $search_form;	
    }
    public function chartDataAction(){
//    	Load model sử dụng trong action

    	Zend_Loader::loadClass('Admin_Models_UserModel');
    	
//    	Tắt layout
		//Zend_debug::dump($this->_arrParam);
    	$this->_helper->layout()->disableLayout();
    	
//    	Khai báo biến
		
    	$data = array();
    	$model = new Admin_Models_UserModel();
    	//Zend_debug::dump($this->_arrParam);
//    	Lấy số lượng user đăng ký theo ngày 
		if($this->_arrParam['ProductID']=='0' && $this->_arrParam['time_search']=='5' ){
		  
    	$reg_user = $model->countUserRegByDay();
		foreach ($reg_user as $day) {

    		$data[] = array($day['day'] * 1000,intval($day['user']));
    	}
		}else{
		    
		    $reg_user = $model->countUserRegByDayBySort($this->_arrParam);
		    foreach ($reg_user as $day) {
		    	$data[] = array($day['day'] * 1000,intval($day['user']));
		    }
		}
    	$data = array_values($data);
    	$this->view->data = json_encode($data);
    }
    
    public function loginLogAction(){
    	
//    	Khai báo biến

    	$db = Zend_Db_Table::getDefaultAdapter();
    	$mySql = $db->select()
    				->from(array('l'	=> login_log),array('UserId','times'	=> 'count(id)'));
    	if ($this->_arrParam['date']) {
    		$mySql->where('DATE(time) = ?', $this->_arrParam['date']);
    	}else{
    		$now = new Zend_Date();
    		$mySql->where('DATE(time) = ?', $now->toString('yyyy-MM-dd'));
    	}
    	$mySql->group('UserId');
    	if ($this->_arrParam['search']){
    		$mySql->where('ui.`' . $this->_arrParam['type'] . '` like ?', "%" . $this->_arrParam['search'] . "%");
    	}
    	$this->setPagination($mySql, $this->_arrParam);
    }
    public function totalLoginChartAction(){
    	
    	$this->view->headScript()->appendFile('/js/highstock.js');
    	
    }
    public function totalLoginChartDataAction(){
    	
//    	Load model sử dụng trong action

    	Zend_Loader::loadClass('Admin_Models_LoginLogModel');
    	
//    	Tắt layout

    	$this->_helper->layout()->disableLayout();
    	
//    	Khai báo biến

    	$data = array();
    	$model = new Admin_Models_LoginLogModel();
    	
//    	Lấy số lượng user đăng ký theo ngày 

    	$total_login_list = $model->countTotalLogByDay();
		foreach ($total_login_list as $day) {
    		$data[] = array($day['day'] * 1000,intval($day['log']));
    	}
    	
    	$data = array_values($data);
    	$this->view->data = json_encode($data);
    }
    public function totalUserLoginChartAction(){
        Zend_Loader::loadClass('Admin_Forms_UserGrowthLoginForm');
        Zend_Loader::loadClass('Admin_Models_UserModel');
        $db_game = new Admin_Models_UserModel();
        $listgame = $db_game->loadlistgame();
    	$this->view->headScript()->appendFile('/js/highstock.js');
    	$arr = array();
    	if($this->_arrParam['time_search']!="0"){
    		$this->_arrParam['Starttime']=null;
    		$this->_arrParam['Endtime']=null;
    	}
    	 
    	$search_form = new Admin_Forms_UserGrowthLoginForm($listgame,array('submit'	=> 'Xem'));
    	$search_form->setDefaults($this->_arrParam);
    	$this->view->arrParams = $this->_arrParam;
    	$this->view->search_form = $search_form;
    	echo 1;
    }
    public function totalUserLoginChartDataAction(){
    	
//    	Load model sử dụng trong action

    	Zend_Loader::loadClass('Admin_Models_LoginLogModel');
    	
//    	Tắt layout

    	$this->_helper->layout()->disableLayout();
    	
//    	Khai báo biến

    	$data = array();
    	$model = new Admin_Models_LoginLogModel();
    	
//    	Lấy số lượng user đăng ký theo ngày 
    	if($this->_arrParam['ProductID']=='0' && $this->_arrParam['time_search']=='5' ){
    	
    		 
    	$total_login_list = $model->countTotalUserLoginByDay();
		foreach ($total_login_list as $day) {
    		$data[] = array($day['day'] * 1000,intval($day['log']));
    	}
    	$data = array_values($data);
    	}else{
    	    $total_login_list = $model->countTotalUserLoginByDayByData($this->_arrParam);
    	    foreach ($total_login_list as $day) {
    	    	$data[] = array($day['day'] * 1000,intval($day['log']));
    	    }
    	    $data = array_values($data);
    	}
    	
    	$this->view->data = json_encode($data);
    }
    public function compareGameAction(){
        Zend_Loader::loadClass('Admin_Forms_UserCompareForm');
        Zend_Loader::loadClass('Admin_Models_UserModel');
        $db_game = new Admin_Models_UserModel();
        $listgame = $db_game->loadlistgame();
        $this->view->headScript()->appendFile('/js/highcharts.js');
         
         
        //Zend_debug::dump($this->_arrParam);
        //die;
        // 		die;
        //    	Khai báo biến
        $arr = array();
        if($this->_arrParam['time_search']!="0"){
        	$this->_arrParam['Starttime']=null;
        	$this->_arrParam['Endtime']=null;
        }
        $search_form = new Admin_Forms_UserCompareForm($listgame,array('submit'	=> 'Xem'));
        $search_form->setDefaults($this->_arrParam);
        $this->view->arrParams = $this->_arrParam;
        $this->view->search_form = $search_form;
    }
    public function compareGameBoxChartAction(){
        //    	Load model sử dụng trong action
        
        Zend_Loader::loadClass('Admin_Models_UserModel');
         
        //    	Tắt layout
        //Zend_debug::dump($this->_arrParam);
        $this->_helper->layout()->disableLayout();
         
        //    	Khai báo biến
        
        $data = array();
        $model = new Admin_Models_UserModel();
        //Zend_debug::dump($this->_arrParam);
        //    	Lấy số lượng user đăng ký theo ngày
    
        if(!isset($this->_arrParam['Starttime'])){
            $start = date('Y-m-d',time());
            $limit  = 10;
            $rangelimit = array();
            $rangelimit[] = $start;
            for($i = 1;$i<$limit;$i++){
            	$date = strtotime($start);
            	$rangelimit[$i] =  date('Y-m-d',$date);
            	$start =  date('Y-m-d',$date-86400);
            }
             
            
            $data = array();
            $j=0;
            $load_list_game  = $model->loadtopgamebytime($start);
            
            foreach ($load_list_game as $item){
            	if($j<$limit){
            		$data[] = $model->selectdatabyday($item,$rangelimit);
            		$j++;
            	}else{
            		$result  = $model->selectdatabyday2($item,$rangelimit);
            		$data[$limit]['name']="Khác";
            		for($i=0;$i<$limit;$i++){
            			$data[$limit]['data'][$i]=$data[$limit]['data'][$i]+$result['data'][$i];
            		}
            	}
            }
          
            
        }else{
          
          //  echo 1;
            if(!isset($this->_arrParam['limit'])){
                $this->_arrParam['limit'] = 7;
            }
            $start = $this->_arrParam['Starttime'];
            $limit  = $this->_arrParam['limit'];
            $rangelimit = array();
            $rangelimit[] = $start;
            for($i = 1;$i<$limit;$i++){
            	$date = strtotime($start);
            	$rangelimit[$i] =  date('Y-m-d',$date-86400);
            	$start =  date('Y-m-d',$date-86400);
            }
         
            
            $data = array();
            $j=0;
            $load_list_game  = $model->loadtopgamebytime($start);

            foreach ($load_list_game as $item){
                if($j<$limit){
                	$data[] = $model->selectdatabyday($item,$rangelimit);
                	$j++;
                }else{
                    $result  = $model->selectdatabyday2($item,$rangelimit);
                    $data[$limit]['name']="Khác";
                    for($i=0;$i<$limit;$i++){
                    	$data[$limit]['data'][$i]=$data[$limit]['data'][$i]+$result['data'][$i];
                    }
                }
            }
           
        }

		
		$data = array_values($data);
		$array_final = preg_replace('/\"([0-9]*)\"/', '$1:', json_encode($data));
	    $this->view->data = json_encode($data);
    }
    public function compareGameBoxAction(){
    	Zend_Loader::loadClass('Admin_Forms_UserCompareBoxForm');
    	Zend_Loader::loadClass('Admin_Models_UserModel');
    	$db_game = new Admin_Models_UserModel();
    	$listgame = $db_game->loadlistgame();
    	$this->view->headScript()->appendFile('/js/highcharts.js');
    	 
    	 
    	//Zend_debug::dump($this->_arrParam);
    	//die;
    	// 		die;
    	//    	Khai báo biến
    	$arr = array();
    	
    //	Zend_debug::dump($this->_arrParam);
    	$search_form = new Admin_Forms_UserCompareBoxForm($listgame,array('submit'	=> 'Xem'));
    	$search_form->setDefaults($this->_arrParam);
    	$this->view->arrParams = $this->_arrParam;
    	$this->view->search_form = $search_form;
    	
    }
    public function compareGameChartAction(){
    	//    	Load model sử dụng trong action
    
    	Zend_Loader::loadClass('Admin_Models_UserModel');
    	 
    	//    	Tắt layout
    	//Zend_debug::dump($this->_arrParam);
    	$this->_helper->layout()->disableLayout();
    	 
    	//    	Khai báo biến
    
    	$data = array();
    	$model = new Admin_Models_UserModel();
    	//Zend_debug::dump($this->_arrParam);
    	//    	Lấy số lượng user đăng ký theo ngày
    	if($this->_arrParam['ProductID']=='0' && $this->_arrParam['time_search']=='5' ){
    
    		$reg_user = $model->countUserRegByDayWithProduct();
    		 
    		$i=0;
    		foreach ($reg_user as $day) {
    			if($i<10){
    				$db = Zend_Db_Table_Abstract::getDefaultAdapter();
    				$sql = $db->select()->from('product',array('Title'))->where('ProductID='.$day['ProductID']);
    				$title = $db->fetchRow($sql);
    				if(!isset($title['Title'])) $title['Title']= 'Default';
    				$data[] = array($title['Title'],intval($day['user']));
    				$i++;
    			}else{
    				$data[10]=array($data[10]['0']='Khác',$data[10]['1']+intval($day['user']));
    			}
    		}
    	}else{
    
    		$reg_user = $model->countUserRegByDayWithProductByData($this->_arrParam);
    		$i=0;
    		foreach ($reg_user as $day) {
    			if($i<10){
    				$db = Zend_Db_Table_Abstract::getDefaultAdapter();
    				$sql = $db->select()->from('product',array('Title'))->where('ProductID='.$day['ProductID']);
    				$title = $db->fetchRow($sql);
    				if(!isset($title['Title'])) $title['Title']= 'Default';
    				$data[] = array($title['Title'],intval($day['user']));
    				$i++;
    			}else{
    				$data[10]=array($data[10]['0']='Khác',$data[10]['1']+intval($day['user']));
    			}
    		}
    	}
    	$data = array_values($data);
    	$this->view->data = json_encode($data);
    }
    function buildUserGrownAction(){
        Zend_Loader::loadClass('Admin_Models_UserModel');
        
        //    	Tắt layout
        //Zend_debug::dump($this->_arrParam);
        $this->_helper->layout()->disableLayout();
        
        //    	Khai báo biến
        $ini_config = new Zend_Config_Ini(APPLICATION_PATH . "/configs/startcount.ini",
        		null,
        		array('skipExtends'        => true,
        				'allowModifications' => true));
         
        if (!isset($ini_config->START_USER_BUILD)){
        	$count_start = '2013-03-05';
        }else{
        	$count_start = $ini_config->START_USER_BUILD;
        }
         
        $data = array();
        $model = new Admin_Models_UserModel();
		$db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        $day_end  = $ini_config->START_USER_BUILD;
      	$day_start = date('Y-m-d');
      	$limit = (strtotime($day_start)-86400-strtotime($day_end))/86400;
      	for($j=1;$j<=$limit;$j++){
      	    
      	$time_count = strtotime($day_end)+$j*86400;
      	$i=0;
      	$data_game_byday = $model->countUserRegByDayWithProductByOneDay(date('Y-m-d',$time_count));
      	//Zend_Debug::dump($data_game_byday);
      	$k=0;
      	foreach($data_game_byday as $day){
      	    	$db = Zend_Db_Table_Abstract::getDefaultAdapter();
      	    	$sql = $db->select()->from('product',array('Title'))->where('ProductID='.$day['ProductID']);
      	    	$title = $db->fetchRow($sql);
      	    	if(!isset($title['Title'])) $title['Title']= 'Default';
      	    	$data = array('Title'=>$title['Title'],'Total'=>intval($day['user']),'ProductID'=>$day['ProductID'],'Day'=>date('Y-m-d',$day['day']));
      	    	$db->insert('userdata', $data);    
      	    	$k++;

      	}
		if(sizeof($data_game_byday)>0){
      	$ini_config->START_USER_BUILD =date('Y-m-d',$time_count);
      	$writer = new Zend_Config_Writer_Ini(array('config'   => $ini_config,
      			'filename' => APPLICATION_PATH . '/configs/startcount.ini'));
      	$writer->write();
		}
      	}
      	echo 'Completed';
      	
    }
}
